package com.mallcai.bigdata.ladon.controller.peekdata;

import com.cake.bigdata.starter.exception.category.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mallcai.bigdata.ladon.controller.peekdata.form.PeekForm;
import com.mallcai.bigdata.ladon.controller.peekdata.form.PeekForm.*;
import com.mallcai.bigdata.ladon.dpl.entity.peekdata.Peek;
import com.mallcai.bigdata.ladon.dpl.entity.peekdata.PeekField;
import com.mallcai.bigdata.ladon.dpl.entity.peekdata.PeekRule;
import com.mallcai.bigdata.ladon.service.peekdata.IPeekFieldService;
import com.mallcai.bigdata.ladon.service.peekdata.IPeekQueryService;
import com.mallcai.bigdata.ladon.service.peekdata.IPeekService;
import com.mallcai.bigdata.ladon.utils.CollectionUtils;
import com.mallcai.bigdata.ladon.vo.DataVo;
import com.mallcai.bigdata.ladon.vo.MyPageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by oneape<oneape15@163.com>
 * Created 2019-05-22 17:09.
 * Modify:
 */
@RequestMapping("/peekdata/peek")
@RestController
public class PeekManageController {

    @Autowired
    private IPeekService iPeekService;
    @Autowired
    private IPeekFieldService iPeekFieldService;
    @Autowired
    private IPeekQueryService iPeekQueryService;

    @RequestMapping("/add")
    public String addModel(@RequestBody @Validated(value = AddCheck.class) PeekForm form) {
        Peek peek = form.toEntity();
        List<PeekRule> rules = form.getRules();
        if (rules == null || rules.isEmpty()) {
            throw new BusinessException("取数规则不能为空");
        }

        int status = iPeekService.addPeek(peek, rules);
        if (status <= 0) {
            throw new BusinessException("添加取数失败");
        }
        return "添加取数成功";
    }

    @RequestMapping("/edit")
    public String editModel(@RequestBody @Validated(value = EditCheck.class) PeekForm form) {
        Peek peek = form.toEntity();
        List<PeekRule> rules = form.getRules();
        if (rules == null || rules.isEmpty()) {
            throw new BusinessException("取数规则不能为空");
        }

        int status = iPeekService.editPeek(peek, rules);
        if (status <= 0) {
            throw new BusinessException("修改取数失败");
        }

        return "修改取数成功";
    }

    @RequestMapping("/del")
    public String delModel(@RequestBody @Validated(value = IdCheck.class) PeekForm form) {
        Peek model = form.toEntity();
        int status = iPeekService.deletePeek(model);
        if (status <= 0) {
            throw new BusinessException("删除模型失败");
        }

        return "删除模型成功";
    }

    @RequestMapping("/getById")
    public Peek getModelById(@RequestBody @Validated(value = IdCheck.class) PeekForm form) {
        Peek peek = iPeekService.getById(form.getModelId());
        if (peek == null) {
            throw new BusinessException("删除模型失败");
        }

        return peek;
    }

    @RequestMapping("/getByPage")
    public MyPageInfo getByPage(@RequestBody @Validated PeekForm form) {
        PageHelper.startPage(form.getCurrentPage(), form.getPageSize());
        List<Peek> models = iPeekService.getByParams(form.getParams());
        PageInfo<Peek> pageInfo = new PageInfo<>(models);
        return new MyPageInfo<>(pageInfo);
    }


    /**
     * 给当前用户推送数据
     *
     * @param form PeekForm
     * @return ApiResult
     */
    @RequestMapping("/sendData2Me")
    public String sendData2Me(@RequestBody @Validated(value = IdCheck.class) PeekForm form) {
        Long peekId = form.getPeekId();
        Peek peek = iPeekService.getById(peekId);
        int status;
        if (StringUtils.isNotEmpty(peek.getFields())){
            status = iPeekService.sendData2Me(peekId);
        } else {
            status = iPeekQueryService.sendData2Me(peekId);
        }

        if (status > 0) {
            return "发送邮件成功";
        }
        return "导出数据失败, 请稍后再试";
    }


    /**
     * 计算数据条数
     *
     * @param form PeekForm
     * @return ApiResult
     */
    @RequestMapping("/countSize")
    public Long countSize(@RequestBody @Validated(value = ModelIdCheck.class) PeekForm form) {
        Long modelId = form.getModelId();
        List<PeekRule> rules = form.getRules();
        Long size;
        if(CollectionUtils.isNotEmpty(form.getFieldList())){
            size = iPeekQueryService.countDataSize(modelId,form.getFieldList(), rules);
        } else {
            size =  iPeekService.countDataSize(modelId, rules);
        }
        return size;
    }

    /**
     * @param form PeekForm
     * @return ApiResult
     */
    @RequestMapping("/previewData")
    public DataVo previewData(@RequestBody @Validated(value = PreviewDataCheck.class) PeekForm form) {
        List<PeekField> fieldList = form.getFieldList();
        DataVo dataVo;
        if (CollectionUtils.isNotEmpty(fieldList)){
            dataVo = iPeekQueryService.previewData(form.getModelId(), form.getFieldList(), form.getRules());
        } else {
            dataVo = iPeekService.previewData(form.getModelId(), form.getFields(), form.getRules());
        }
        return dataVo;
    }

    /**
     * @param form PeekForm
     * @return ApiResult
     */
    @RequestMapping("/getRuleByPeekId")
    public List<PeekRule> getRuleByPeekId(@RequestBody @Validated(value = IdCheck.class) PeekForm form) {
        return iPeekService.getRuleByPeekId(form.getPeekId());
    }


    /**
     * 获取所有数据类型映射的规则
     *
     * @param form PeekForm
     * @return ApiResult
     */
    @RequestMapping("/getDataTypeRules")
    public Map getDataTypeRules(@RequestBody @Validated PeekForm form) {
        return iPeekService.getAllDataRule();
    }

    @RequestMapping("/saveQuery")
    public String saveQuery(@RequestBody @Validated(value = SaveCheck.class) PeekForm form) {
        Peek peek = form.toEntity();
        List<PeekField> fieldList = form.getFieldList();
        if (CollectionUtils.isEmpty(fieldList)){
            throw new BusinessException("取数字段不能为空");
        }
        int status = iPeekService.saveQuery(peek);
        if (status <= 0) {
            throw new BusinessException("添加取数失败");
        }

        return "添加取数成功";
    }

    /**
     * @param form PeekForm
     * @return ApiResult
     */
    @RequestMapping("/queryExistedRuleAndFieldList")
    public Map<String, Object> queryExistedRuleAndFieldList(@RequestBody @Validated(value = IdCheck.class) PeekForm form) {
        Map<String, Object> result = new HashMap<>(2);
        List<PeekRule> rules = iPeekService.getRuleByPeekId(form.getPeekId());
        List<PeekField> fieldList = iPeekFieldService.getByPeekId(form.getPeekId());
        result.put("ruleList", rules);
        result.put("fieldList", fieldList);
        return result;
    }
}
